﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace AutoExecuteSp.AutoExecuteSp
{
    public partial class MainForm : Form
    {
        private List<ExecuteResult> executeResults = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();

            BinaryFormatter binaryFormatter = new BinaryFormatter();

            using (FileStream fs = File.OpenRead(args[1]))
            {
                executeResults = (List<ExecuteResult>)binaryFormatter.Deserialize(fs);
                fs.Close();
            }

            foreach (var executeResult in executeResults)
            {
                listView1.Items.Add(
                    new ListViewItem(new string[]
                                         {
                                             executeResult.FileInfo.FullName, executeResult.IsSuccess.ToString(),
                                             executeResult.Message,
                                             executeResult.Exception == null
                                                 ? string.Empty
                                                 : executeResult.Exception.Message
                                         }));
            }

            listView1.Tag = 0;
        }

        private void ListView1DoubleClick(object sender, EventArgs e)
        {
            foreach (ListViewItem selectItem in listView1.SelectedItems)
            {
                ProcessStartInfo ps = new ProcessStartInfo(selectItem.Text)
                                        {
                                            UseShellExecute = true,
                                            Verb = "open"
                                        };
                Process.Start(ps);
            }
        }

        class ListViewItemComparer : IComparer
        {
            private readonly int col;
            private readonly SortOrder order;

            public ListViewItemComparer()
            {
                col = 0;
                order = SortOrder.Ascending;
            }

            public ListViewItemComparer(int column, SortOrder order)
            {
                col = column;
                this.order = order;
            }

            public int Compare(object x, object y)
            {
                int returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                               ((ListViewItem)y).SubItems[col].Text);

                if (order == SortOrder.Descending)
                    returnVal *= -1;

                return returnVal;
            }
        }

        private void ListView1ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column != (int)listView1.Tag)
            {
                listView1.Tag = e.Column;
                listView1.Sorting = SortOrder.Ascending;
            }
            else
            {
                listView1.Sorting = listView1.Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
            }

            listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, listView1.Sorting);

            listView1.Sort();
        }
    }
}
